package com.bluestation.net.http;

/*****************************************************************
 *
 * Author: Partha Kuchana
 * Date : 23rd August 2001
 * Description: This is the main class for the Web Server application.
 *
 * WebServer.java	1.0 	23/08/2001 	Partha Kuchana
 *
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this block appears in all copies. 
 *
 * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 ******************************************************************/

import java.net.*;
import java.io.*;
import java.util.*;

public class WebServer
{
	private static final String	PROPERTIES_FILE			= "WebServer.properties";
	private static final String	PARAM_HOME_DIRECTORY	= "HOME_DIRECTORY";
	private static final String	PARAM_LOG_FILE			= "LOG_FILE";
	private static final String	PARAM_PORT_NUMBER		= "PORT_NUMBER";
	public static String		HOME_DIRECTORY;
	public static String		LOG_FILE;
	public static int			PORT_NUMBER;

	public static void main(String[] args) throws IOException
	{
		ServerSocket serverSocket = null;
		boolean listening = true;

		// Read the properties file
		if (!(WebServer.getProperties()))
		{
			System.out
					.println(" Unable to locate the Server properties file \n"
							+ " Or Parameter missing. \n  Process stopped.");
			System.exit(-1);
		}

		// Open the server socket
		try
		{
			serverSocket = new ServerSocket(WebServer.PORT_NUMBER);
		}
		catch (IOException e)
		{
			System.err.println("Unable to start the server on port "
					+ WebServer.PORT_NUMBER);
			System.err
					.println("Port "
							+ WebServer.PORT_NUMBER
							+ "  is in use. Please terminate the "
							+ " process currently using the port and restart WebSever ");
			WSUtil.log("Unable to start the server on port "
					+ WebServer.PORT_NUMBER + ". Process stopped");
			System.exit(-1);
		}

		System.out.println(" Web Server Started Successfully");
		WSUtil.log("Server started successfully");

		// Start listening for incoming requests.
		while (listening)
		{
			Socket Client = null;
			try
			{
				Client = serverSocket.accept();
			}
			catch (Exception ex)
			{
				System.out.println(ex.getMessage());
				System.exit(1);
			}

			//Create an instance of ClientRequest to process the current request.
			try
			{
				new ClientRequest(Client).start();
			}
			catch (Exception e)
			{
				System.out
						.println("Unable to connect to the client for sending results.\n"
								+ " Client might have reset the connection ");

				WSUtil
						.log("Unable to connect to the client for sending results.\n"
								+ " Client might have reset the connection. ");
			}
		}

		serverSocket.close();
	}

	/**
	 * This method retrieves properties from the property file
	 * @return boolean - true, if the file exits and parameters are specified. 
	 *					false, otherwise.
	 */
	private static boolean getProperties()
	{

		Properties p = new Properties();
		String value = null;

		try
		{
			p.load(new FileInputStream(new File(WebServer.PROPERTIES_FILE)));
			if ((value = p.getProperty(WebServer.PARAM_HOME_DIRECTORY)) != null)
			{
				WebServer.HOME_DIRECTORY = value;
			}
			if ((value = p.getProperty(WebServer.PARAM_LOG_FILE)) != null)
			{
				WebServer.LOG_FILE = value;
			}
			if ((value = p.getProperty(WebServer.PARAM_PORT_NUMBER)) != null)
			{
				WebServer.PORT_NUMBER = new Integer(value).intValue();
			}
		}
		catch (Exception e)
		{
			return false;
		}
		return true;
	}
}
